﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MVCAddressBook.Models.Models;
using MVCAddressBook.Models.Interfaces;
using MVCAddressBook.Models.Entities;

namespace MVCAddressBook.Controllers
{
    public class HomeController : Controller
    {

        public IUserService userService { get; set; }
        public HomeController() : this(null) { }
        public HomeController(IUserService  service)
        {
            userService = service ?? new UserService();
        }
        //
        // GET: /Home/

        public ActionResult Index()
        {
            IList<UserInfo> userlist = userService.GetUserList();
            return View(userlist);
        }
        public ActionResult About()
        {
            return View();
        }
        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            UserInfo user = userService.GetUser(id);
            return View(user);
        }

        //
        // GET: /Home/Create
        [Authorize(Users="admin")]
        public ActionResult CreateUser()
        {
            UserInfo user = new UserInfo();

            return View();
        } 

        //
        // POST: /Home/Create
        [Authorize]
        [HttpPost]
        public ActionResult CreateUser(FormCollection collection)
        {
            
            UserInfo user = new UserInfo();

            user.Name = Request.Form["Name"];
            user.Mobile = Request.Form["Mobile"];
            user.Phone = Request.Form["Phone"];
            user.QQ = Request.Form["QQ"];
            user.Email = Request.Form["Email"];
            user.Address = Request.Form["Address"];
            user.Company = Request.Form["Company"];
            user.HeadImg = Request.Form["HeadImg"];

            if(userService.AddUser(user))
            {
                TempData["message"] = "修改用户资料成功";
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
            
        }
        
        //
        // GET: /Home/Edit/5
         [Authorize]
        public ActionResult Edit(int id)
        {
            UserInfo user = userService.GetUser(id);
           
                return View(user);
            
        }

        //
        // POST: /Home/Edit/5
         [Authorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            UserInfo user = new UserInfo();
            UpdateModel(user);
            if (userService.EditUser(user))
            {
                TempData["message"] = "修改用户资料成功";
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Home/Delete/5
         [Authorize]
        public ActionResult Delete(int id)
        {
            UserInfo user = userService.GetUser(id);
            return View(user);
        }

        //
        // POST: /Home/Delete/5
         [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
           
            if (userService.DelUser(id))
            {
                TempData["message"] = "删除用户资料成功";
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
          
        }

        public ActionResult Error()
        {
            return View();
        }
    }
}
